package yxy.game.pm2.active.command.actpay;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actweekdiscount;
import yxy.cherry.data.bean.charge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_每周特惠;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.DataIdOrderId;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.message.PayMessage.WeekPayInfoRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付活动 - 每周特惠  
 * @author Dell
 *
 */
@MessageType(proto = Command.WeekPayInfo, classtype = PayMessage.WeekPayInfoRequest.class)
public class CMD_Active_每周特惠_Info extends Command<WeekPayInfoRequest> {
	
	private static final Logger logger = Logger.create(CMD_Active_每周特惠_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, WeekPayInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();

		PayMessage.WeekPayInfoResponse.Builder response = PayMessage.WeekPayInfoResponse.newBuilder();
		
		try {
			handle_每周特惠(messageSource, response, pcode);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		send(messageSource, response, Result.DataError);
	}

	private void send(MessageSource messageSource, PayMessage.WeekPayInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
	public void handle_每周特惠(MessageSource messageSource, PayMessage.WeekPayInfoResponse.Builder response, String pcode) {
		ActiveModule_每周特惠 am = ActiveModule_每周特惠.get(pcode,false);

		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		ArrayList<DataIdOrderId> finishs = am.getFinish();
		ArrayList<DataIdOrderId> locks = am.getLock();

		Map<String, actweekdiscount> awcs = ActDataCenter.getAllData(actweekdiscount.class, pcode);
		for(actweekdiscount awc : awcs.values()) {
			PayMessage.ActivePay.Builder build = PayMessage.ActivePay.newBuilder();
			
			int chargeid = awc.getChargeid();
			charge goodPrice = DataCenter.getData(chargeid, charge.class); //商品价格
			int limit = awc.getNum();
			int[] rewardtypes = awc.getRewardtype();
			int[] rewardids = awc.getRewardid();
			int[] rewardnums = awc.getRewardnum();
			
			build.setGoodsid(chargeid);  //商品id
			build.setLimit(limit);

			build.setGoodsPrice(goodPrice.getDismoney()); //商品价格
			build.setRebate(awc.getRebate()); //返利倍数
			
			Collection<Currency> curs = Currency.create(rewardtypes, rewardids, rewardnums);
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			
			build.addAllCur(items);
			
			int finish = 0;
			for(int i = 0; i < finishs.size(); i++) {
				DataIdOrderId finishdio = finishs.get(i);
				if(finishdio.getDataid() == chargeid) {
					HashMap<String, DateTime> odmap = finishdio.getMyorderinfoMap();
					if(odmap == null) {
						continue;
					}
					finish = odmap.size();
				}
			}
			
			int lock = 0;
			for (int i = 0; i < locks.size(); i++) {
				DataIdOrderId lockdio = locks.get(i);
				if(lockdio.getDataid() == chargeid) {
					HashMap<String, DateTime> odmap = lockdio.getMyorderinfoMap();
					if(odmap == null) {
						continue;
					}
					
					lock = odmap.size();
				}
			}
			
			int canbuynum = limit - finish - lock;
			canbuynum = canbuynum < 0? 0:canbuynum;
			
			build.setCanbuynum(canbuynum);
			response.addAps(build.build());
		}
		
		response.setEnd(am.getRoundend().getMillis());
		send(messageSource, response, Result.Success);
	}
	

	
}
